﻿hpxAdminApp.controller('queryOrderController', function ($scope, $rootScope, $state, API_URL, exportService, NgTableParams, payService, payingService, orderService, searchService) {
    var emptyEntity = {};
    var newEntity = angular.copy(emptyEntity, newEntity);
    console.log($rootScope)
    $scope.filter = {
        'func': 'simple',
        'deadlineTime1':'',
        'deadlineTime2': '',
        duty:''
    };

    $("#start_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language:  'zh-CN'
    }).on("click",function(ev){
        $("#start_time").datetimepicker("setEndDate", $("#end_time").val());
    }).on('changeDate', function(e) {
        $scope.filter.deadlineTime1 = $("#start_time").val();
    });

    $("#end_time").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language:  'zh-CN'
    }).on("click", function (ev) {
        $("#end_time").datetimepicker("setStartDate", $("#start_time").val());
    }).on('changeDate', function(e) {
        $scope.filter.deadlineTime2 = $("#end_time").val();
    });

    //获取所有正在进行中的订单
    $scope.tableParams = new NgTableParams({ sorting: { 'id': 'desc' } }, {
        getData: function (params) {
            return orderService.getAllOrders(params, $scope.filter).then(function (data) {
                $scope.first = $scope.getFirst(params);
                return data;
            });
        }
    });
    // 刷新
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }

    $scope.check = function (id) {
        orderService.getOne(id.toString()).then(function (data) {
            $scope.model = data;
        });
    //弹出详细窗口
        $('#modal-check').modal('show');
        $('.jqzoom').imagezoom();
    };

    $scope.showAppraisal = function (item) {
        searchService.getEnterpriseAppraisal(item,{func:'order'}).then(function (result) {
            $scope.model = result;
            $('#modal-appraisal').modal('show');
        });
    };

    //签收
    $scope.signfor = function (item) {
        console.log(item)
        if (confirm('确认签收吗？')) {
            payingService.signFor(item.id).then(function (result) {
                alert("签收成功");
                $scope.tableParams.reload();
            })
        }
    }
    // 重发签收
    $scope.sendAgain = function (item) {
        if (confirm('确认重新签收吗？')) {
            payingService.signSecondFor(item.id).then(function (result) {
                console.log(result)
                alert("签收成功");
                $scope.tableParams.reload();
            })
        }
    }

    $scope.save = function (data) {
        $scope.model = angular.copy(data);       //弹出终止窗口
        $('#modal-read').modal('show');
    };

    $scope.stopOrder = function () {
        if (!$scope.model.description) {
            alert("请填写终止原因");
            return;
        }
        //if (!$scope.model.duty) {
        //    alert("请选择主动终止方");
        //    return;
        //}
        //终止订单
        if (confirm('确认终止此订单吗？')) {
            //console.log("asdasdasd");
            //console.log($scope.model)
            orderService.stopOrder($scope.model).then(function (data) {
                
                $('#modal-read').modal('hide');
                if (confirm('订单终止成功！')) {
                    $scope.tableParams.reload();
                }
            });
        }
    };

    $scope.syncOrder = function (enterpriseId, orderId) {
        payingService.searchOrder(enterpriseId, orderId).then(function (data) {
            alert("同步成功");
            $scope.tableParams.reload();
        });

    }



    //导出表格
    $scope.exportExcel = function () {
        var resource_url = API_URL + "/forExcelExport/order";
        var sheet_name = "订单信息表";
        var label_names, label_types, label_keys;
        var condition="";
        if ($scope.filter.deadlineTime1 != null && $scope.filter.deadlineTime1 != "") {
            condition += "&sTime=" + $scope.filter.deadlineTime1;
        }
        if ($scope.filter.deadlineTime2 != null && $scope.filter.deadlineTime2 != "") {
            condition += "&eTime=" + $scope.filter.deadlineTime2;
        }
        if ($scope.filter.tradeTypeCode != null && $scope.filter.tradeTypeCode != "") {
            condition += "&tradeTypeCode=" + $scope.filter.tradeTypeCode;
        }
        if (condition != null && condition != "") {
            resource_url += ("?" + condition.substr(1));
        }
        label_names = ["交易时间", "订单编号", "票号", "卖方企业", "买方企业", "票据金额","交易金额", "手续费", "服务费（优惠后）", "优惠券（优惠前）", "业务模式", "活动备注"];
        label_types = ["String", "String", "String", "String", "String", "Decimal", "Decimal", "String", "String", "String", "String", "String"];
        label_keys = ["order_time", "order_number", "bill_number", "drawer_enterprise", "receiver_enterprise", "bill_sum_price", "order_total_price", "fee", "bre","endd",
            "trade_type", "description"];

        var excelRequest = {
            "resource_url": resource_url,
            "resource_name": "order",
            "sheet_name": sheet_name,
            "label_names": label_names,
            "label_types": label_types,
            "label_keys": label_keys
        };
        var token = '';
        if ($rootScope.identity != undefined) {
            token = $rootScope.identity.token;
        }
        exportService.exportExcel('export', token, excelRequest).then(function (result) {
            if (result.data.meta.code == 200) {
                var newWin = window.open('loading page');
                newWin.location.href = result.data.data;
            } else {
                alert(result.data.meta.message);
            }

        })
    }

});